﻿@{
    ViewBag.Title = "文档类别管理";
}


<div class="container">
    <ol class="breadcrumb">
        <li><a href="/xt/index">系统配置</a></li>
        <li class="active">菜单配置</li>
    </ol>

    <div class="panel panel-primary">
        <div class="panel-heading">菜单管理</div>
        <div class="panel-body form-inline">
            <div class="btn-group">
                @Html.MyButton("btn_add_cate", "增加菜单")
                @Html.MyButton("btn_ren_cate", "重命名")
                @Html.MyButton("btn_del_cate", "移除菜单")
                @Html.MyButton("btn_linkto_gn", "关联功能")
                @Html.MyButton("btn_setting", "样式设置")
            </div>

            <div class="btn-group">
                @Html.MyButton("btn_collapse_expand_all", "收缩/展开全部")
                @Html.MyButton("btn_order_set", "排序设置")
            </div>

            <p id="nowaddone" style="margin-top: 30px;" class="hidden">
                您目前没有设置菜单，现在<a href="#" onclick="javascript:$('#btn_add_cate').click();">设置</a>一个吧!
            </p>
        </div>

        <ul class="list-group" id="cate_conetent">
        </ul>

    </div>
</div>

@section script{
    <script>

        $(function () {
            $('#btn_add_cate').popover({
                html: true,
                placement: "bottom",
                content: "<label>菜单名称:<input type='text' name='catename' id='catename'></label>" +
                    "<div class='btn-group'>" +
                    "<button class='btn btn-primary btn-sm' id='btn_save_add_cate' onclick='saveAddpopover()'>保存</button>" +
                    "<button class='btn btn-default btn-sm' id='btn_close_add_cate' onclick='closeAddpopover()'>关闭</button>" +
                    "</div>"
            }).on('shown.bs.popover', function () {
                $("#catename").focus();
            });


            function getSelectedEle() {
                var li = $("#cate_conetent .selected-bgc");
                if (li.size() == 0) {
                    mainfn.msg("请选择一行菜单");
                    return null;
                }
                return li;
            }

            //收缩/展开全部
            $("#btn_collapse_expand_all").click(function () {
                $("#cate_conetent > li").each(function (idx, obj) {
                    if ($(obj).data("lvl") != '0') {
                        $(obj).slideToggle();
                    }
                });
            });

            $("#btn_ren_cate").click(function () {
                var $li = getSelectedEle();
                if ($li) {
                    var text = $li.find("label").text();
                    $li.find("label").before("<input type='text' data-ori='" + text + "' value='" + text + "'>&nbsp;&nbsp;<a href='#' onclick='saverename(this)'>保存</a>&nbsp;&nbsp;<a href='#' onclick='revo(this)'>撤销</a>").remove();
                    $li.find(":text:first").select();
                }
            });

            $("#btn_order_set").click(function () {
                $("#btn_collapse_expand_all").click();
                var txt = $("#btn_order_set").text();
                if (txt == '排序设置') {
                    $("#btn_order_set").attr("class", "btn btn-primary").text("保存排序");
                    $("#cate_conetent > li").each(function (idx, obj) {
                        $(obj).prepend("<input type='text' data-menuid='" + $(obj).data("cateno") + "' style='width:40px;text-align:center' value='" + idx + "'>");
                    });
                } else {
                    $("#btn_order_set").attr("class", "btn btn-default").text("排序设置");
                    var ids = "", orders = "";
                    $("#cate_conetent :text").each(function (i, input) {
                        ids += $(input).data("menuid") + ",";
                        orders += input.value + ",";
                        $(input).remove();
                    });
                    $.post("/xt/savemenuorder", { ids: ids, orders: orders }, function (data) {
                        if (data.flag) {
                            init();
                        } else {
                            mainfn.msg(data.msg);
                        }

                    });
                }
            });

            $("#btn_del_cate").click(function () {
                var $li = getSelectedEle();
                if ($li) {
                    removecate($li.children(".close"));
                }
            });

            $("#btn_setting").click(function () {

                var $li = getSelectedEle();
                if (!$li) {
                    return;
                }

                var menuid = $li.data("cateno");
                if (!menuid) {
                    mainfn.msg("请新刷界面生重试");
                    return;
                }

                mainfn.window("/xt/menusetting?menuid=" + menuid, {
                    title: "菜单样式设置",

                    buttons: [
                        {
                            handler: function () {
                                mainfn.window("getwin").saveResult();
                            },
                            text: "保存"
                        }, {
                            handler: function () {
                                mainfn.window("close");
                            },
                            text: "关闭"
                        }
                    ]
                });
            });

            $("#btn_linkto_gn").click(function () {

                var $li = getSelectedEle();
                if (!$li) {
                    return;
                }

                var menuid = $li.data("cateno");
                if (!menuid) {
                    mainfn.msg("请新刷界面生重试");
                    return;
                }

                mainfn.window("/xt/menulinktofunction?menuid=" + menuid, {
                    title: "关联功能",
                    width: 800,
                    height: 500,
                    buttons: [
                        {
                            handler: function () {
                                mainfn.window("close");
                            },
                            text: "关闭"
                        }
                    ]
                });
            });

            function init(parameters) {
                $("#cate_conetent").load("/xt/getrescategrid", {}, function () {
                    bindCateElementClick();
                    $("#btn_collapse_expand_all").click();
                });
            }

            init();
        });

        // 绑定列表单击事件
        function bindCateElementClick() {
            var $li = $("#cate_conetent > li");
            if ($li.size() == 0) {
                $("#nowaddone").removeClass("hidden");
            } else {
                $li.unbind("click").bind("click", function () {
                    var li = $(this);

                    li.addClass("selected-bgc").siblings().removeClass("selected-bgc");

                    if (li.data("lvl") == '0') {
                        li.nextUntil("[data-lvl='0']").slideToggle();
                    }
                });
            }

        }

        // 撤销重命名
        function revo(t) {
            var $li = $(t).parent();
            var text = $li.children(":text").data("ori");
            $li.children(".close").siblings().remove().end().end().prepend("<label>" + text + "</label>");
        }

        // 保存重命名
        function saverename(t) {
            var $li = $(t).parent();
            var $text = $li.children(":text");
            var text = $text.val();
            if (text == "") {
                $text.tooltip({ title: "请填写菜单名称", placement: "auto" });
                $text.tooltip('show');
                setTimeout(function () {
                    $text.tooltip('destroy');
                }, 2000);
            } else {
                if ($text.data("ori") != text) {
                    var cateno = $li.data("cateno");
                    $.post("/xt/renamecatename", { cateno: cateno, newname: text }, function (data) {
                        if (data.flag) {
                            $text.data("ori", text);
                            revo($text);
                        }
                        mainfn.msg(data.msg);
                    });
                }
                $text.data("ori", text);
                revo($text);
            }
        }

        // 移除菜单
        function removecate(t) {
            var cateno = $(t).parent().data("cateno");

            if (cateno) {
                $.post("/xt/removecate", { cateno: cateno }, function (data) {
                    mainfn.msg(data.msg);

                    if (data.flag) {
                        $(t).parent().remove();
                    }
                });
            } else {
                mainfn.msg("请先列表中选中一条记录后再操作，或者点击行末尾的删除按钮");
            }
        }

        // 新增菜单
        function saveAddpopover() {
            var catename = $("#catename").val(),
                orderno = $("#cate_conetent").children("li").size(),
                parentno = "ROOT",
                level = 0,
                obj = {};
            var $sel = $("#cate_conetent").children(".selected-bgc");

            if ($sel.size() == 1) {
                parentno = $sel.data("cateno");
                level = $sel.data("lvl") * 1 + 1;
            }

            if (level == 2) {
                mainfn.msg("目前只能新增两级菜单");
                return;
            }

            obj = { catename: catename, orderno: orderno, parentno: parentno, level: level };

            $.post("/xt/addcategory", obj, function (data) {

                if (data.flag) {
                    $("#nowaddone").size() && $("#nowaddone").remove();

                    var newlevel = 0;
                    if ($sel.size() != 0) {
                        newlevel = ($sel.data("lvl") * 1 + 1);
                        $sel.after('<li class="list-group-item" data-cateno="' + data.msg + '" data-lvl="' + newlevel + '" data-gnid="NONE" style="margin-left:' + newlevel * 50 + 'px;"><label>' + catename + '</label>\
                    <span class="pull-right close" onclick="removecate(this);">&times;</span></li>');
                    } else {
                        $("#cate_conetent").append('<li class="list-group-item" data-cateno="' + data.msg + '" data-gnid="NONE" data-lvl="' + newlevel + '" style="margin-left:' + newlevel * 50 + 'px;"><label>' + catename + '</label>\
                    <span class="pull-right close" onclick="removecate(this);">&times;</span></li>');
                    }
                    bindCateElementClick();

                    mainfn.msg("菜单新增成功");
                } else {
                    mainfn.msg(data.msg);
                }
            });

            closeAddpopover();
        }

        function closeAddpopover() {
            $('#btn_add_cate').popover('toggle');
        }


    </script>
}
